
var gulp = require('gulp'),
    minifycss = require('gulp-minify-css'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
    del = require('del');
   imagesmin=require('gulp-imagemin');
    livereload = require('gulp-livereload');

//压缩css
gulp.task('minify_css',["clean"], function () {
    var cssSrc = ['public/css/**/*.css'];
    return gulp.src(cssSrc)	  //压缩的文件
        .pipe(concat('all.css'))	//合并所有css到all.css
        .pipe(gulp.dest('dist'))   //输出文件夹
        .pipe(rename({suffix: '.min'}))
        .pipe(minifycss())
        .pipe(gulp.dest('dist'));	//执行压缩且存放的位置
});

//压缩js
gulp.task('minify_js',["clean"], function() {
    var jsSrc = ['public/js/**/*.js'];
    return gulp.src(jsSrc)
        .pipe(concat('all.js'))	//合并所有js到all.js
        .pipe(gulp.dest('dist'))	//输出all.js到文件夹
        .pipe(rename({suffix: '.min'}))   //rename压缩后的文件名
        .pipe(uglify())	//压缩
        .pipe(gulp.dest('dist'))  //输出
        .pipe(livereload());
});


gulp.task('watch', function() {
    livereload.listen(); //要在这里调用listen()方法
    gulp.watch('public/js/**/*.js', ['minify_js']);
});

//压缩图片
gulp.task('imagemin',["clean"], function() {
    var imgSrc = ['public/**/*.png'];
    return gulp.src(imgSrc)
        .pipe(imagesmin())	//压缩
        .pipe(gulp.dest('dist/imgmin'));  //输出
});



//执行压缩前，先删除以前压缩的文件
gulp.task('clean', function() {
    return del(['dist/**/all.css', 'dist/**/all.min.css', 'dist/**/all.js', 'dist/**/all.min.js'])
});




// 默认任务
gulp.task('default', function(){
    gulp.run('minify_css', 'minify_js');

});


